package com.linlong.linlongchat.controller;

import com.linlong.linlongchat.domain.dto.FriendDto;
import com.linlong.linlongchat.domain.po.Friend;
import com.linlong.linlongchat.domain.po.Result;

import com.linlong.linlongchat.service.IFriendService;
import com.linlong.linlongchat.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 联系人 前端控制器
 * </p>
 *
 * @author 华少
 * @since 2024-09-19
 */
@RestController
@RequestMapping("/friend")
@Api(tags = "好友管理接口")
public class FriendController {
    @Autowired
    private IFriendService friendService;

    @GetMapping("/list")
    @ApiOperation("查询好友列表")
    public Result<List<Friend>> getFriendList() {
        Map<String, Object> claims= ThreadLocalUtil.get();
        String userAccount = (String)claims.get("account");
        List<Friend> friendList = friendService.getFriendList(userAccount);
        return Result.ok(friendList);
    }

    // 添加好友接口
    @PostMapping("/add")
    @ApiOperation("添加好友")
    public Result addFriend(@RequestBody FriendDto friendDto) {
        boolean success = friendService.addFriend(friendDto);
        if (success) {
            return Result.ok("添加好友成功");
        } else {
            return Result.fail("添加好友失败");
        }
    }

    @GetMapping("/findFriend")
    @ApiOperation("查询好友关系")
    public Result<Boolean> findFriend(@RequestParam("friendAccount") String friendAccount){
        Map<String, Object> claims= ThreadLocalUtil.get();
        String userAccount = (String)claims.get("account");
        Boolean flag = friendService.findFriend(userAccount,friendAccount);
        return Result.ok(flag);
    }


    @PutMapping("/updateRemark")
    @ApiOperation("修改好友备注")
    public Result updateRemark(@RequestBody FriendDto friendDto){
        friendService.updateRemark(friendDto);
        return Result.ok("修改好友备注成功");
    }

    @PutMapping("/deleteFriend/{friendAccount}")
    @ApiOperation("删除好友")
    public Result deleteFriend(@PathVariable String friendAccount){
        friendService.deleteFriend(friendAccount);
        return Result.ok("删除好友成功");
    }
}



